libname dario "G:\data\workdata\707562";

DATA idapall;
set dario.idapall;
run;

DATA idapall;
set idapall;
if type="H";
keep pnr lonind arledgr sumgrad erhver kom alder arbgnr cvrnr akom koen pstill aar timelon lbnr joblon  tlonkval HELTID_DELTID_KODE
DISCOALLE_INDK DISCO08_ALLE_INDK ie_type OPR_LAND hfaudd;
run;


DATA tenure;
set idapall;
keep pnr  aar cvrnr;
run;


proc sort data=tenure;
by pnr cvrnr;
run;


DATA tenure;
set tenure;
count +1;
by pnr cvrnr;
if first.pnr or first.cvrnr then count=1;
run;


DATA tenure;
set tenure;
rename aar=year;
rename count=tenure;
run;


DATA tenure;
set tenure;
year2=year+0;
drop year;
rename year2=year;
run;


DATA tenure;
set tenure;
if year>=1993;
run;


proc sort data=tenure;
by pnr year;
run;


DATA idapall;
set idapall;
rename aar=year;
bopkom=input(akom, 12.);
run;


DATA idapall;
set idapall;
if year>="1993";
run;

DATA idapall;
set idapall;
alder2=alder+0;
year2=year+0;
drop year;
drop alder;
rename year2=year;
rename alder2=age;
run;


DATA idapall;
set idapall;
if pstill>="31" & pstill<="37";
male=koen="1";
rename erhver=work_exp;
if pstill="31" then manager=1;
else manager=0;
if pstill="32" or pstill="33" or pstill="34" then ml=1;
else ml=0;
rename sumgrad=unemp_rate_1980;
rename arledgr=unemp_rate_current;
if HELTID_DELTID_KODE="5" or HELTID_DELTID_KODE="6" or HELTID_DELTID_KODE="7" or HELTID_DELTID_KODE="8" then partime=1;
else partime=0;
run;

proc sort data=idapall;
by pnr year;
run;


data idapall2;
merge idapall (in=a) tenure (in=b);
by pnr year;
if a and b;
run;


data dario.idapall3;
set idapall2;
run;



/*****RETRIVE ADDITIONAL INFO ON PART-TIMERS

DATA idan;
set dario.idan;
run;


DATA idan;
set idan;
if type="H";
keep pnr aar tilknyt;
run;



DATA idan;
set idan;
if aar>=2007;
run;

DATA dario.idan2;
set idan;
run;

*/


****from firm we retrive firm accountings and firm industry;

DATA firm;
set dario.firm;
run;


DATA firm;
set firm;
rename aar=year;
run;


DATA firm;
set firm;
year2=year+0;
drop year;
rename year2=year;
run;


DATA firm;
set firm;
if year>=1995;
run;

DATA firm;
set firm;
keep cvrnr year  GF_BRANCHE_93  GF_BRANCHE_03 GF_BRANCHE_07 GF_OMS GF_EGUL GF_AINV;
run;



proc sort data=firm;
by cvrnr year;
run;

data industry_1;
set firm;
run;


DATA industry_2;
set dario.industry_95_98;
rename STORDB93=GF_BRANCHE_93;
rename STORDB03=GF_BRANCHE_03;
rename GF_EGUL_1=GF_EGUL;
rename GF_AINV_1=GF_AINV;
rename GF_OMS_1=GF_OMS;
drop STORDB07;
run;

data industry_3;
set industry_1 industry_2;
run;


*****THIS DATASET industry_93_94 is constructed from use "G:\Data\Workdata\703989\STATA FILES\firm2.dta";


DATA industry_4;
set dario.industry_93_94;
rename GF_E_GUL=GF_EGUL;
rename aar=year;
run;


DATA industry_4;
set industry_4;
year2=year+0;
drop year;
rename year2=year;
run;


data industry;
set industry_3 industry_4;
run;


data industry;
set industry;
run;


data dario.industry;
set industry;
run;


proc sort data=dario.industry;
by cvrnr year;
run;


DATA idapall2;
set idapall2;
cvrnr2=cvrnr+0;
drop cvrnr;
rename cvrnr2=cvrnr;
run;


/****merge with allida****/
proc sort data=idapall2;
by cvrnr year;
run;


data dario.idapall3;
merge idapall2 (in=a) dario.industry (in=b);
by cvrnr year;
if a and b;
run;




DATA uhdi;
set dario.uhdi;
run;

DATA uhdi;
set uhdi;
keep land vare aar senr cvrnr vrd ie;
run;

/*ie 2 means that we look at export values, ie 1 is in fact import*/

DATA uhdi;
set uhdi;
if ie="2";
year2=aar+0;
drop aar;
rename year2=year;
run;


DATA uhdi;
set uhdi;
run;



proc sort data=dario.industry;
by cvrnr year;
run;


proc sort data=uhdi;
by cvrnr year;
run;

data export_ind_yes_merge;
merge dario.industry (in=a) uhdi (in=b);
by cvrnr year;
if a and b;
run;



data export_ind_no_merge;
merge dario.industry (in=a) uhdi (in=b);
by cvrnr year;
if b;
run;


DATA export_ind_no_merge;
set export_ind_no_merge;
rename cvrnr=cvrnr_old;
run;

proc sort data=export_ind_no_merge;
by senr;
run;

DATA senr_cvrnr;
set dario.senr_cvrnr;
run;


proc sort data=senr_cvrnr;
by senr;
run;


data export_ind_no_merge_2;
merge export_ind_no_merge (in=a) senr_cvrnr (in=b);
by senr;
if a and b;
run;


proc sort data=export_ind_no_merge_2;
by cvrnr year;
run;


data export_ind_no_merge_3;
merge export_ind_no_merge_2 (in=a) dario.industry (in=b);
by cvrnr year;
if a and b;
run;


data dario.export_ind;
set export_ind_yes_merge export_ind_no_merge_3;
run;



/*ie 2 means that we look at export values, ie 1 is in fact import*/


DATA uhdi;
set dario.uhdi;
run;

DATA uhdi;
set uhdi;
keep land vare aar senr cvrnr vrd ie;
run;

DATA uhdi;
set uhdi;
if ie="1";
year2=aar+0;
drop aar;
rename year2=year;
run;


DATA uhdi;
set uhdi;
if year>=1993;
run;

proc sort data=dario.industry;
by cvrnr year;
run;


proc sort data=uhdi;
by cvrnr year;
run;

data import_ind_yes_merge;
merge dario.industry (in=a) uhdi (in=b);
by cvrnr year;
if a and b;
run;



data import_ind_no_merge;
merge dario.industry (in=a) uhdi (in=b);
by cvrnr year;
if b;
run;


DATA import_ind_no_merge;
set import_ind_no_merge;
rename cvrnr=cvrnr_old;
run;

proc sort data=import_ind_no_merge;
by senr;
run;

DATA senr_cvrnr;
set dario.senr_cvrnr;
run;


proc sort data=senr_cvrnr;
by senr;
run;


data import_ind_no_merge_2;
merge import_ind_no_merge (in=a) senr_cvrnr (in=b);
by senr;
if a and b;
run;


proc sort data=import_ind_no_merge_2;
by cvrnr year;
run;


data import_ind_no_merge_3;
merge import_ind_no_merge_2 (in=a) dario.industry (in=b);
by cvrnr year;
if a and b;
run;


data dario.import_ind;
set import_ind_yes_merge import_ind_no_merge_3;
run;

